<template>
  <div id="container" ref="map">

  </div>
</template>

<script>
export default {
  methods: {
    createMap() {
      var map = new AMap.Map('container', {
        resizeEnable: true,
        zoom: 10
      })
      this.$store.dispatch('getCurrentPosition', map)
    }
  },
  created() {

    // this.createMap()
  },
  mounted() {
    let _this = this

    var map = new AMap.Map('container', {
      resizeEnable: true,
      zoom: 13
    })
    map.plugin(['AMap.Scale'], function () {
      var scale = new AMap.Scale({});
      map.addControl(scale);
    });

    map.plugin('AMap.Geolocation', function () {
      var geolocation = new AMap.Geolocation({
        enableHighAccuracy: true,// 是否使用高精度定位，默认：true
        convert:true,
        showMarker:true,
        showCircle: false,
        panToLocation:true,
        timeout: 10000
      });

      geolocation.getCurrentPosition();
      map.addControl(geolocation);
      AMap.event.addListener(geolocation, 'complete', onComplete)
      AMap.event.addListener(geolocation, 'error', onError)

      function onComplete(data) {
        // data是具体的定位信息
        console.log(data);
        _this.$store.state.position.address = data.formattedAddress
        _this.$store.state.position.longitude = data.position.lng
        _this.$store.state.position.latitude = data.position.lat
        // console.log(data.formattedAddress);
        // console.log('纬度：' + data.position.getLat());
        // console.log('经度：' + data.position.getLng());
      }

      function onError(data) {
        // 定位出错
      }
    })
  }
}
// export default {
//   methods: {
//     location() {
//       console.log('map')
//       var map = new AMap.Map('container', {
//         resizeEnable: true,
//         zoom: 12,
//         center: [116.480983, 40.0958]
//       })
//     }
//   },
//   mounted() {
//     this.$store.dispatch('get_location2',this.$refs.map.id)
//   }
// }
</script>
<style>
  #container {
    overflow: hidden;
    width: 100%;
    height: 110%;
    margin: 0;
    font-family: "微软雅黑";
  }
</style>
